define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'openapi/message/index' + location.search,
                    add_url: 'openapi/message/add',
                    edit_url: 'openapi/message/edit',
                    del_url: 'openapi/message/del',
                    multi_url: 'openapi/message/multi',
                    import_url: 'openapi/message/import',
                    table: 'tongzhi',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                showToggle: false, // 浏览模式切换
                showColumns: true, // 允许选择显示字段
                showExport: true, // 导出选项
                exportDataType: "basic", //导出当前页
                exportTypes: ['excel'],
                searchFormVisible: false, // 通用搜索 默认是否显示
                search: false, // 是否关闭搜索
                trimOnSearch: true, // 自动删除搜索空格
                pageList: [10, 20, 30, 50],
                sortName: 'id',
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var field = $('.panel-heading .nav-tabs').data('field');
                    if (!filter[field]) {
                        filter[field] = "1";
                        op[field] = '=';
                    } else {
                        filter[field] = '2';
                        op[field] = '=';
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [{
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: __('Id')
                        },
                        {
                            field: 'status',
                            title: "",
                            width: '40px',
                            formatter: function (val) {
                                if (val == "1") {
                                    return '<img class="img-center" width="15px" src="/assets/img/read.png">';
                                } else {
                                    return '<img class="img-center" width="15px" src="/assets/img/notread.png"><span class="cricle"></span>';
                                }
                            },
                            align: 'left',
                            operate: false
                        },
                        {
                            field: 'title',
                            title: __('Title'),
                            // align: 'left',
                            operate: false,
                            formatter: function (value, data, index) {
                                return ' <a href="openapi/message/view?ids=' + data.id + '" style="color:rgba(0, 0, 0, 0.7)" title="查看消息" class="btn-dialog">' + data.title + '</a>';
                            }
                        },
                        {
                            field: 'createtime',
                            title: __('Createtime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {
                                "1": __('Type 1'),
                                "2": __('Type 2')
                            },
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var field = $(this).closest("ul").data("field");
                var value = $(this).data("value");
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var filter = {};
                    if (value !== '') {
                        filter[field] = value;
                    }
                    params.filter = JSON.stringify(filter);
                    return params;
                };
                // table.bootstrapTable('refresh', {});
                return false;
            });
            $(".commonsearch-table").remove()
            $(".fixed-table-toolbar").remove()
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        view: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});